package com.tjzx.struts;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.ActionSupport;

import com.tjzx.common.Cons;
import com.tjzx.common.DocType;
import com.tjzx.common.NewsType;
import com.tjzx.common.Pager;
import com.tjzx.common.Utils;
import com.tjzx.entity.EipNewsInfo;
import com.tjzx.service.EipNewsInfoService;
import com.tjzx.entity.EipProductType;
import com.tjzx.entity.EipProductTypeId;
import com.tjzx.service.EipProductTypeService;

public class MainAction extends ActionSupport {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		WebApplicationContext ctx = this.getWebApplicationContext();
		EipNewsInfoService ts = (EipNewsInfoService)ctx.getBean("eipNewsInfoService");
		String method = request.getParameter("method");
		if (Utils.isEmpty(method)) {
			return executeIndex(mapping, form, request, response, ts);
		} else if (method.equals("all")){
			return executeDisplayAll(mapping, form, request, response, ts);
		} else if (method.equals("get")){
			return executeDisplayNews(mapping, form, request, response, ts);
		} else if (method.equals("suggest")){
			return mapping.findForward("suggest");
		} else if (method.equals("vip")){
			return mapping.findForward("vip");
		} else if (method.equals("reserve")){
			EipProductTypeService tsp = (EipProductTypeService)ctx.getBean("eipProductTypeService");
			return executeReserve(mapping, form, request, response, ts,tsp);
		}
		return mapping.findForward("error");
	}
	public ActionForward executeReserve(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response,EipNewsInfoService ts,EipProductTypeService tsp) {
		Pager<EipProductType> pager = null;
		Pager<EipNewsInfo> pagerNews = null;
		List<EipProductType> bigTypeList = null;
		StringBuilder sbBigTypestr = new StringBuilder();
		StringBuilder sbSubTypestr = new StringBuilder();
		pager = tsp.getTCNews(1, 1, -1, -1);
		bigTypeList = pager.getList();
		
		int count = 0;
		for(EipProductType e:bigTypeList) {
			String subjectId = e.getPtid().toString();
			pagerNews = ts.getTCNews(subjectId,1,1, -1, -1);
			List<EipNewsInfo> newsList = pagerNews.getList();
			String bigTitle = e.getName();
			if(count == 0){
				request.setAttribute("firstSubList", newsList);
			}
			count++ ;
			for(EipNewsInfo f:newsList) {
				String subTitle = f.getSubject().toString();
				sbBigTypestr.append(bigTitle).append("|");
				sbSubTypestr.append(subTitle).append("|");
			}
		}
		request.setAttribute("bigTypeList", bigTypeList);
		request.setAttribute("sbBigTypestr", sbBigTypestr.toString());
		request.setAttribute("sbSubTypestr", sbSubTypestr.toString());
		return mapping.findForward("reserve");
	}
	public ActionForward executeDisplayAll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response,EipNewsInfoService ts) {
		String docType = request.getParameter("docType");
		String newsType = request.getParameter("newsType");
		String pageNo = request.getParameter("pageNo");
		
		//calculate the page no
		int currPageNo = 1;
		if (Utils.isNotEmpty(pageNo)) {
			currPageNo = Integer.valueOf(pageNo);
		}
		Pager<EipNewsInfo> pager = null;
		if (Utils.isEmpty(docType)) {
			if (newsType.equals(NewsType.TCNotes.value())) {
				pager = ts.getTCNotes(currPageNo, Cons.PAGE_SIZE, (currPageNo-1)*Cons.PAGE_SIZE, Cons.PAGE_SIZE);
			} else if (newsType.equals(NewsType.PETCT.value())) {
				pager = ts.getPETCT(currPageNo, Cons.PAGE_SIZE, (currPageNo-1)*Cons.PAGE_SIZE, Cons.PAGE_SIZE);
			} else if (newsType.equals(NewsType.TCFlow.value())) {
				pager = ts.getTCFlow(currPageNo, Cons.PAGE_SIZE, (currPageNo-1)*Cons.PAGE_SIZE, Cons.PAGE_SIZE);
			} else if (newsType.equals(NewsType.TCNews.value())) {
				pager = ts.getTCNews("",currPageNo, Cons.PAGE_SIZE, (currPageNo-1)*Cons.PAGE_SIZE, Cons.PAGE_SIZE);
			}
		} else {
			if (docType.equals(DocType.News.value())) {
				pager = ts.getNews(currPageNo, Cons.PAGE_SIZE, (currPageNo-1)*Cons.PAGE_SIZE, Cons.PAGE_SIZE);
			} else if (docType.equals(DocType.Device.value())) {
				pager = ts.getDeviceNews(currPageNo, Cons.PAGE_SIZE, (currPageNo-1)*Cons.PAGE_SIZE, Cons.PAGE_SIZE);
			} else if (newsType.equals(NewsType.TCNews.value())) {
				pager = ts.getTCNews(docType,currPageNo, Cons.PAGE_SIZE, (currPageNo-1)*Cons.PAGE_SIZE, Cons.PAGE_SIZE);
			} else if (newsType.equals(NewsType.TCNews.value())) {
				pager = ts.getTCNews(docType,currPageNo, Cons.PAGE_SIZE, (currPageNo-1)*Cons.PAGE_SIZE, Cons.PAGE_SIZE);
			} else if (newsType.equals(NewsType.Health.value())) {
				pager = ts.getHealthNews(currPageNo, Cons.PAGE_SIZE, (currPageNo-1)*Cons.PAGE_SIZE, Cons.PAGE_SIZE);
			} else if (newsType.equals(NewsType.HealthManager.value())) {
				pager = ts.getHealthManagerNews(currPageNo, Cons.PAGE_SIZE, (currPageNo-1)*Cons.PAGE_SIZE, Cons.PAGE_SIZE);
			}
		}
		request.setAttribute("docType", docType);
		request.setAttribute("newsType", newsType);
		request.setAttribute("pageNo", pager.getPageNo());
		request.setAttribute("totalPages", pager.getTotalPages());
		request.setAttribute("totalRows", pager.getRowCount());
		request.setAttribute("newsList", pager.getList());
		return mapping.findForward("disMore");
	}
	
	public ActionForward executeDisplayNews(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response,EipNewsInfoService ts) {
		String id = request.getParameter("id");
		EipNewsInfo news = ts.getEipNewsInfoById(Long.valueOf(id));
		String docText = StringEscapeUtils.unescapeHtml(news.getDocText());
		news.setDocText(docText);
		request.setAttribute("news", news);
		return mapping.findForward("display");
	}
	
	public ActionForward executeIndex(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response,EipNewsInfoService ts) {
		List<EipNewsInfo> newsList = ts.getNews(1, Cons.MAIN_DISROWS, -1, Cons.MAIN_DISROWS).getList();
		List<EipNewsInfo> subNewsList = new ArrayList<EipNewsInfo>();
		for(EipNewsInfo e:newsList) {
			if (e.getSubject().length() > 24) {
				String subject = e.getSubject().substring(0, Cons.MAX_DISCHAR_LEN);
				e.setSubject(subject + "...");
			}
			subNewsList.add(e);
		}
		List<EipNewsInfo> healthNewsList = ts.getHealthNews(1, Cons.MAIN_DISROWS, -1, Cons.MAIN_DISROWS).getList();
		List<EipNewsInfo> subHealthNewsList = new ArrayList<EipNewsInfo>();
		for(EipNewsInfo e:healthNewsList) {
			if (e.getSubject().length() > 24) {
				String subject = e.getSubject().substring(0, Cons.MAX_DISCHAR_LEN);
				e.setSubject(subject + "...");
			}
			subHealthNewsList.add(e);
		}
		List<EipNewsInfo> healthManageNewsList = ts.getHealthManagerNews(1, Cons.MAIN_DISROWS, -1, Cons.MAIN_DISROWS).getList();
		List<EipNewsInfo> subHealthManageNewsList = new ArrayList<EipNewsInfo>();
		for(EipNewsInfo e:healthManageNewsList) {
			if (e.getSubject().length() > 24) {
				String subject = e.getSubject().substring(0, Cons.MAX_DISCHAR_LEN);
				e.setSubject(subject + "...");
			}
			subHealthManageNewsList.add(e);
		}
		request.setAttribute("newsList", subNewsList);
		request.setAttribute("healthNewsList", subHealthNewsList);
		request.setAttribute("healthManagerNewsList", subHealthManageNewsList);
		request.setAttribute("deviceNewsList", ts.getDeviceNews(1, Cons.MAIN_DISROWS, -1, Cons.MAIN_DISROWS).getList());
		return mapping.findForward("success");
	}
	
}
